// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import router from './router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import App from './App.vue'
import axios from 'axios'
import Vuex from 'vuex'

import BaiduMap from 'vue-baidu-map'

Vue.use(BaiduMap, {
  ak: '7dAnjfUsghn0cfS9xfTLiaNOWU6CQTlW'
})
Vue.use(Vuex)

/**
 * @description 下面是给store里面的四个字段赋默认值
 * @type {string}
 */
let defaultIsLogin = 'false'
let defaultRole = 'customer'
let defaultAccountId = '11'
let defaultAccountName = ''
try {
  if (localStorage.accountName) {
    defaultAccountName = localStorage.accountName
  }
} catch (e) {

}
try {
  if (localStorage.isLogin) {
    defaultIsLogin = localStorage.isLogin
  }
} catch (e) {

}
try {
  if (localStorage.role) {
    defaultRole = localStorage.role
  }
} catch (e) {

}
try {
  if (localStorage.accountId) {
    defaultAccountId = localStorage.accountId
  }
} catch (e) {

}
const store = new Vuex.Store({
  state: {
    isLogin: defaultIsLogin,
    role: defaultRole,
    accountId: defaultAccountId,
    accountName: defaultAccountName
  },
  getters: {},
  mutations: {
    /**
     * @description 当store commit的时候修改相对应的值
     * @param state store里面的状态值
     * @param payload 要改变的store的值
     */
    setState (state, payload) {
      if (payload.hasOwnProperty('role')) {
        state.role = payload.role
        localStorage.role = payload.role
      }
      if (payload.hasOwnProperty('isLogin')) {
        state.isLogin = payload.isLogin
        localStorage.isLogin = payload.isLogin
        try {
          if (localStorage.isLogin) {
            localStorage.isLogin = payload.isLogin
          }
        } catch (e) {

        }
      }
      if (payload.hasOwnProperty('accountId')) {
        state.accountId = payload.accountId
        localStorage.accountId = payload.accountId
        try {
          if (localStorage.accountId) {
            localStorage.accountId = payload.accountId
          }
        } catch (e) {

        }
      }
      if (payload.hasOwnProperty('accountName')) {
        state.accountName = payload.accountName
        localStorage.accountName = payload.accountName
      }
    }
  }
  // // eslint-disable-next-line no-undef
  // plugins: [createPersistedState()]
})
Vue.config.productionTip = false
Vue.use(ElementUI)
Vue.prototype.$axios = axios

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App),
  template: '<App/>',
  components: {
    App
  }
})
